package com.useeinfo.oa.modules.inspection.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.inspection.dao.InspectionOrderLogDao;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrderLog;
import com.useeinfo.oa.modules.inspection.enums.InsOrderOperationEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class InspectionOrderLogBiz extends CrudBiz<InspectionOrderLogDao, InspectionOrderLog> {
    public void saveInspectionOrderLog(InspectionOrder inspectionOrder, InsOrderOperationEnum orderOperationEnum, HttpServletRequest request){
        SystemUser systemUser= SessionKeyUtils.getSessionUser(request);
        InspectionOrderLog inspectionOrderLog = new InspectionOrderLog();
        inspectionOrderLog.setInspectionOrder(inspectionOrder);
        inspectionOrderLog.setEnterprise(SessionKeyUtils.getSessionEnterprise(request));
        inspectionOrderLog.setSystemUser(systemUser);
        inspectionOrderLog.setUserName(systemUser.getUserName());
        String detail=systemUser.getUserName()+"在"+ DateTimeUtils.formatDateToStringWithTime(new Date())+","
                +orderOperationEnum.getName()+"，操作工单名称为："+inspectionOrder.getOrderName()+",编号为："+inspectionOrder.getId();
        inspectionOrderLog.setDetail(detail);
        inspectionOrderLog.setOrderOperationEnum(orderOperationEnum);
        dao.add(inspectionOrderLog);
    }
    public void saveInspectionOrderLogByCache(InspectionOrder inspectionOrder, InsOrderOperationEnum orderOperationEnum, SystemUser systemUser, Enterprise enterprise,Date createDate){
        InspectionOrderLog inspectionOrderLog = new InspectionOrderLog();
        inspectionOrderLog.setInspectionOrder(inspectionOrder);
        inspectionOrderLog.setEnterprise(enterprise);
        inspectionOrderLog.setSystemUser(systemUser);
        inspectionOrderLog.setUserName(systemUser.getUserName());
        String detail=systemUser.getUserName()+"在"+ DateTimeUtils.formatDateToStringWithTime(new Date())+","
                +orderOperationEnum.getName()+"，操作工单名称为："+inspectionOrder.getOrderName()+",编号为："+inspectionOrder.getId();
        inspectionOrderLog.setDetail(detail);
        inspectionOrderLog.setOrderOperationEnum(orderOperationEnum);
        inspectionOrderLog.setCreateDate(createDate);
        dao.add(inspectionOrderLog);
    }
}
